module ReportsHelper
  def reports_index_fields(options)
    %w[authored sourcename author content pertinent incidents]
  end
  
  def reports_format_field(obj, field, options)
    case field
    when "sourcename"
      link_to(obj.send(field), edit_source_path(obj.source_id))
    when "incidents"
      (obj.incidents.collect{|i| link_to("#{i.id_str}", edit_incident_path(i))}.join("&nbsp;") + "&nbsp;" +
        link_to("+", "#", :onclick => "show_picker(#{obj.id}); return false;")).html_safe
    when "authored"
      obj.authored_at ?
        (content_tag("div", :class => "ago"){time_ago_in_words2(obj.authored_at)} +
          content_tag("div", :class => "exact"){obj.authored_at.strftime("%e-%b-%Y %H:%M")}).html_safe : ""
    when "content"
      cont = (content_tag("strong"){obj.title ? obj.title + ": " : ""} + strip_tags(obj.content)).html_safe
      content_tag("div"){obj.url ? link_to(cont, obj.url) : cont}
    when "pertinent"
      link_to(obj.important || "?", "#", :onclick => "report_change_importance(this, #{obj.id}); return false;")
    else
      obj.send(field)
    end
  end
  
  def reports_index_links(objs, options)
    []
  end
  
  def time_ago_in_words2(time)
    return "" if time.nil?
    str = time_ago_in_words(time)
    if str == "less than a minute"
      str = "just now"
    else
      str = str.gsub("about ", "").gsub("minute", "min").gsub("hour", "hr") + " ago"
    end
    str
  end
end
